<?php
/**
 * manage blog with comment
 * @author Tuong Tran <tuong.tran@outlook.com>
 */

//import blog model
Yii::import('common.models.blog.*');

class BlogController extends Controller {

    /**
     * @var CActiveRecord the currently loaded data model instance.
     */
    private $_model;

    public function actions() {

        return array(
            'delete' => array(
                'class' => 'DeleteAction',
                'modelClass' => 'Blog'
            )
        );
    }

    public function actionIndex(){
        $model = new Node;

        $this->render('index', array(
            'model' => $model
        ));
    }

    public function actionCreate() {
        $model = new Blog;

        if (isset($_POST['Blog'])) {
            $model->attributes = $_POST['Blog'];

            if($model->save()){
                Yii::app()->user->setFlash('success', 'New post created successfully');

                //check meta
                if(isset($_POST['featuredImage']) && $_POST['featuredImage'] != ''){
                    $meta = new NodeMeta;
                    $meta->meta_key = 'featured_image';
                    $meta->meta_value = $_POST['featuredImage'];
                    $model->addMetaData($meta);
                }

                //redirect to update
                $this->redirect(array('update', 'id' => $model->node_id));
            }
        }

        $this->render('create', compact('model'));
    }

    public function actionUpdate() {

        $model = $this->loadModel();

        if(isset($_POST['Blog'])){
            $model->attributes = $_POST['Blog'];
            if($model->save()){
                //check featured image
                if($_POST['featuredImage'] == '' && $model->featuredImage != NULL){
                    //remove
                    $model->featuredImage->delete();
                }elseif($_POST['featuredImage'] != ''){
                    if($model->featuredImage == NULL){ //empty featured image, create new meta
                        $model->featuredImage = new NodeMeta;
                        $model->featuredImage->meta_key = 'featured_image';
                    }
                    //update value
                    $model->featuredImage->node_id = $model->node_id;
                    $model->featuredImage->meta_value = $_POST['featuredImage'];
                    $model->featuredImage->save();
                }

                Yii::app()->user->setFlash('success', 'Update post successfully');
            }
        }


        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete() {

        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request

            $this->loadModel()->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

            if (!isset($_GET['ajax']))
                $this->redirect(array('index'));
        }

        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     */
    public function loadModel() {

        if ($this->_model === null) {

            if (isset($_GET['id'])) {

                if (Yii::app()->user->isGuest)
                    $condition = 'status=' . Blog::STATUS_PUBLISHED . ' OR status=' . Blog::STATUS_ARCHIVED;

                else
                    $condition = '';

                $this->_model = Blog::model()->findByPk($_GET['id'], $condition);
            }

            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }

        return $this->_model;
    }
}